package com.woupla.company;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.enterprise.inject.New;
import javax.faces.bean.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.joda.time.DateTime;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

import com.woupla.account.CurrentUserManager;
import com.woupla.city.CityMBean;
import com.woupla.model.Adress;
import com.woupla.model.Company;
import com.woupla.model.ExceptionPlaning;
import com.woupla.model.Planing;
import com.woupla.model.User;
import com.woupla.rdv.RdvEJB;

/**
 * 
 */

/**
 * @author Dimitri Lebel
 * 
 */
@Named
@ViewScoped
public class CompanyController implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -8912254295304875243L;
	@Inject
	private CompanyEJB companyEJB;
	@Inject
	@New
	private Company company;
	@Inject
	@New
	private Adress adress;
	@Inject
	private CityMBean cityMBean;
	
	@Inject
	private CurrentUserManager currentUserManager;
	
	@Inject
	private RdvEJB rdvEJB;
	
	private List<User> employees;
	
	private PlaningSemaine semaine;
	
	private User userSelected;
	
	private LazyDataModel<ExceptionPlaning> exceptions;
	private ExceptionPlaning exceptionPlaningSelected;
	private Date ExceptionHeureDeb;
	private Date ExceptionHeureFin;
	
	
	public CompanyController() {
		semaine = new PlaningSemaine();
		exceptionPlaningSelected = new ExceptionPlaning();
		exceptions = new LazyDataModel<ExceptionPlaning>() {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = -3941791555340763771L;

			@Override
			public List<ExceptionPlaning> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
				return rdvEJB.findExceptionPlaning(company, first, pageSize, sortField, sortOrder, filters);
			}
		};
	}
	@PostConstruct
	public void init() {
		company.setAdress(adress);
		employees = searchEmployees();
	}

	public void create() {
		company.getAdress().setCity(cityMBean.getCitySelected());
		companyEJB.saveAdress(company);
		//companyEJB.saveCompany(company);
	}
	
	public List<User> searchEmployees(){	
		employees = companyEJB.FindCompanyEmployees(currentUserManager.getCurrentAccount().getCompany());
		return employees ;
		
	}
	public void findEmployeePlaning(){
		Planing planing = rdvEJB.findStaticUserPlaning(userSelected);
		semaine.setPlaning(planing);		
	}
	
	public void savePlaningStatic(){
		rdvEJB.saveStaticUserPlaning(semaine.getPlaning());
	}
	public void createExceptionPlaning(){
		DateTime debut = new DateTime(exceptionPlaningSelected.getDateDebut());
		DateTime heure = new DateTime(ExceptionHeureDeb);
		debut = debut.withHourOfDay(heure.getHourOfDay());
		debut = debut.withMinuteOfHour(heure.getMinuteOfHour());
		
		DateTime fin = new DateTime(exceptionPlaningSelected.getDateFin());
		heure = new DateTime(ExceptionHeureFin);
		debut = debut.withHourOfDay(heure.getHourOfDay());
		debut = debut.withMinuteOfHour(heure.getMinuteOfHour());
	}
	
	public void newExceptionPlaning(){
		exceptionPlaningSelected = new ExceptionPlaning();
		exceptionPlaningSelected.setCompanyid(company);
	}
	/**
	 * @return the companyEJB
	 */
	public CompanyEJB getCompanyEJB() {
		return companyEJB;
	}

	/**
	 * @param companyEJB
	 *            the companyEJB to set
	 */
	public void setCompanyEJB(CompanyEJB companyEJB) {
		this.companyEJB = companyEJB;
	}

	/**
	 * @return the company
	 */
	public Company getCompany() {
		return company;
	}

	/**
	 * @param company
	 *            the company to set
	 */
	public void setCompany(Company company) {
		this.company = company;
	}



	/**
	 * @return the semaine
	 */
	public PlaningSemaine getSemaine() {
		return semaine;
	}

	/**
	 * @param semaine the semaine to set
	 */
	public void setSemaine(PlaningSemaine semaine) {
		this.semaine = semaine;
	}
	/**
	 * @return the userSelected
	 */
	public User getUserSelected() {
		return userSelected;
	}
	/**
	 * @param userSelected the userSelected to set
	 */
	public void setUserSelected(User userSelected) {
		this.userSelected = userSelected;
	}
	/**
	 * @return the exceptions
	 */
	public LazyDataModel<ExceptionPlaning> getExceptions() {
		return exceptions;
	}
	/**
	 * @param exceptions the exceptions to set
	 */
	public void setExceptions(LazyDataModel<ExceptionPlaning> exceptions) {
		this.exceptions = exceptions;
	}
	/**
	 * @return the exceptionPlaningSelected
	 */
	public ExceptionPlaning getExceptionPlaningSelected() {
		return exceptionPlaningSelected;
	}
	/**
	 * @param exceptionPlaningSelected the exceptionPlaningSelected to set
	 */
	public void setExceptionPlaningSelected(ExceptionPlaning exceptionPlaningSelected) {
		this.exceptionPlaningSelected = exceptionPlaningSelected;
	}
	/**
	 * @return the employees
	 */
	public List<User> getEmployees() {
		return employees;
	}
	/**
	 * @param employees the employees to set
	 */
	public void setEmployees(List<User> employees) {
		this.employees = employees;
	}
	/**
	 * @return the exceptionHeureDeb
	 */
	public Date getExceptionHeureDeb() {
		return ExceptionHeureDeb;
	}
	/**
	 * @param exceptionHeureDeb the exceptionHeureDeb to set
	 */
	public void setExceptionHeureDeb(Date exceptionHeureDeb) {
		ExceptionHeureDeb = exceptionHeureDeb;
	}
	/**
	 * @return the exceptionHeureFin
	 */
	public Date getExceptionHeureFin() {
		return ExceptionHeureFin;
	}
	/**
	 * @param exceptionHeureFin the exceptionHeureFin to set
	 */
	public void setExceptionHeureFin(Date exceptionHeureFin) {
		ExceptionHeureFin = exceptionHeureFin;
	}
}
